//
//  CYRechargeWayViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/22.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYRechargeWayViewController.h"
#import "CYTotalRechargeModel.h"
#import "CYTransferViewController.h"
#import "CYRechargeHeaderView.h"
#import "CYDataSourceDelegate.h"
#import "RechargeViewController.h"
@interface CYRechargeWayViewController (){
    CYDataSource *_datasource;
    CYDelegate *_delegate;
    
}
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) CYRechargeHeaderView *headerView;
@property (nonatomic, strong) NSDictionary *personal_data;//个人中心信息
@property (nonatomic, strong) NSString *personal_plistName;//个人信息plist名称
@property (nonatomic, strong) NSMutableDictionary *recharge_dict;//转账信息
@property (nonatomic, copy) NSString *rechargePlistName;//转账信息plist名称
@property (nonatomic, strong) CYTotalRechargeModel *totalmodel;
@end

@implementation CYRechargeWayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _personal_plistName = @"personal";
    _rechargePlistName = @"recharge.plist";
    self.headerView.hidden = NO;
    self.tableview.hidden = NO;
    // Do any additional setup after loading the view.
}

- (CYRechargeHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[CYRechargeHeaderView alloc] init];
        [self.view addSubview:_headerView];
        
        [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(NAVIBAR_HEIGHT + STATUS_HEIGHT, 0,
                                                   SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - _headerView.bounds.size.height, 0));
        }];
    }
    return _headerView;
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]init];
        [self setupTableview];
        [self.view addSubview:_tableview];
        
        [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(_headerView.mas_bottom).with.offset(0);
            make.bottom.equalTo(self.view).with.offset(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
    }
    return _tableview;
}
- (void)setupTableview{
    WS(self)
    _datasource = [[CYDataSource alloc]initWithData:[self getCellSource] identifier:@"CYTableViewCell" block:^(id cell, id data) {
        [weakSelf bindCell:cell dict:data];
    }];
    
    _delegate = [[CYDelegate alloc]initWithCellHeight:AUTO(44) views:nil block:^(NSIndexPath *index) {
        [weakSelf selectedCell:index];
    }];
    
    _tableview.dataSource = _datasource;
    _tableview.delegate = _delegate;
    _tableview.scrollEnabled = NO;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (void)bindCell:(CYTableViewCell *)cell dict:(NSDictionary *)dict{
    cell.imageView.image = [UIImage imageNamed:dict[@"image"]];
    cell.textLabel.text = dict[@"title"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

- (void)selectedCell:(NSIndexPath *)index{
    if (index.row == 3){
        CYTransferViewController *transferVC = [[CYTransferViewController alloc]init];
        [self.navigationController pushViewController:transferVC animated:YES];
    }
    else{
        RechargeViewController *rechargeVC = [[RechargeViewController alloc]init];
        rechargeVC.type = index.row + 1;
        rechargeVC.model = [self getDictWithIndex:index.row];
        rechargeVC.personalData = _personal_data;
        [self.navigationController pushViewController:rechargeVC animated:YES];
    }
}
- (void)setPersonal_data:(NSDictionary *)personal_data{
    if (!personal_data)
        _personal_data = [CYCache getCache:_personal_plistName];
    else if (_personal_data != personal_data)
        _personal_data = personal_data;
    [CYCache cache:_personal_data plist:_personal_plistName];
    self.headerView.imageName = @"portrait";
    self.headerView.userName = _personal_data[@"username"];
    self.headerView.money = [_personal_data[@"coin"] floatValue];//余额
}

- (void)setRecharge_dict:(NSMutableDictionary *)recharge_dict{
    if (!recharge_dict)
        _recharge_dict = [CYCache getCache:_rechargePlistName];
    else if (_recharge_dict != recharge_dict)
        _recharge_dict = recharge_dict;
    [CYCache cache:_recharge_dict plist:_rechargePlistName];
}

//根据选的支付方式，筛选出相应的数据
- (id)getDictWithIndex:(NSInteger)index{
    if (index == 0)
        return _totalmodel.wx;//微信
    else if (index == 1)
        return _totalmodel.zfb;//支付宝
    else if (index == 5)
        return _totalmodel.qqqb;//QQ钱包
    else
        return nil;
}

- (NSArray *)getCellSource{
    return @[@{@"title":@"微信充值",@"image":@"WeChat"},@{@"title":@"支付宝充值",@"image":@"Alipay"},
             @{@"title":@"在线充值",@"image":@"unionPay"},@{@"title":@"银行汇款",@"image":@"transfer"},
             @{@"title":@"网银快捷充值",@"image":@"czwykjicon"},@{@"title":@"QQ钱包",@"image":@"czqqqbiocn"}];
}

//网络请求
- (void)https{
    
    //个人中心请求
    NSDictionary *para = @{@"username":[CYCache readDataWithKey:@"username"],@"token":[CYCache readDataWithKey:@"token"]};
    self.task = [CYHttps POST:API_personal parameters:para showHUD:NO progress:nil success:^(id data) {
        if (data)
            self.personal_data = data;
        else {
            Class vc  = NSClassFromString(@"CYLoginViewController");
            [self.navigationController pushViewController:[vc new] animated:YES];
        }
    } failure:^(id string) {
        self.personal_data = nil;
    }];
    
    //转账功能请求
    self.spareTask = [CYHttps POST:API_scan parameters:para showHUD:NO progress:nil success:^(id data) {
        if (data)
            self.recharge_dict = data;
        _totalmodel = [CYTotalRechargeModel mj_objectWithKeyValues:data];
    } failure:^(id string) {
        self.recharge_dict = nil;
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self https];
    
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = @"选择充值方式";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
